package br.uefs.dexa.rfframework.gui.config.dialogs;

import java.awt.Color;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.Font;

import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.ListCellRenderer;

public class FeatureListItemRenderer extends JPanel implements ListCellRenderer {

	 private Color selectionColor;
     private Color backgroundColor;
     private Color alternateColor;
	
	 private JProgressBar progressBar;
	 
     private final JLabel description;
     
     private final JLabel statusText;
     
     private final JLabel statusLabel;
     
     
     public FeatureListItemRenderer()
     {
    	 super();
    	 
    	 selectionColor = new Color(203, 228, 248);
         backgroundColor = Color.WHITE;
         alternateColor = new Color(242, 242, 242);
    	 
    	 setLayout(new FlowLayout(FlowLayout.RIGHT));
    	 
    	 description = new JLabel();
         description.setFont(new Font("Tahoma", Font.BOLD, 11));
         
         progressBar = new JProgressBar(0, 100);
         progressBar.setValue(0);
         
         statusText = new JLabel("0%");
         
         statusLabel = new JLabel("Status:");
         
         add(description);
         add(progressBar);
         
    	 
     }
	
     public void setProgressBarLimits(int begin, int end)
     {
    	 progressBar.setMinimum(begin);
    	 progressBar.setMaximum(end);
     }
	
     public void updateProgressBar(int progress)
     {
    	 progressBar.setValue(progress);
     }
     
	@Override
	public Component getListCellRendererComponent(JList list, Object value,
			int index, boolean isSelected, boolean cellHasFocus) {
		
		
		FeatureListItem item = (FeatureListItem) value;
		
		 // Handle coloring
        if (index % 2 == 0)
             setBackground(backgroundColor);
        else
             setBackground(alternateColor);
             
        if (isSelected)
             setBackground(selectionColor);
        
        if (item.isEnabled()) {          
             description.setForeground(Color.BLACK);
             statusLabel.setForeground(Color.BLACK);
             statusText.setForeground(Color.BLACK);
        } else {
             description.setForeground(Color.LIGHT_GRAY);
             statusLabel.setForeground(Color.LIGHT_GRAY);
             statusText.setForeground(Color.LIGHT_GRAY);
        }
		
		
		// Set the textlabels
        description.setText(item.getDescription());
        statusText.setText(item.getMessage());
        
     // Set the progressbar
        int progress = item.getProgress();
        if (0 <= progress && progress <= 100) 
        {
             progressBar.setIndeterminate(false);
             progressBar.setValue(progress);
             //System.out.println(progress);
        }
        
        return this;
		
	}

}
